from utils import *


SIGNATURE_TYPE_RSA = 'rsa'

"""generic signature class used in information, cmd, etc."""
class signature:
    def __init__(self,type=None,signature=None):
        self._type = type
        self._signature = signature
        
    def dumps(self):
        if self._type == SIGNATURE_TYPE_RSA:
            return "%s:%s"%(SIGNATURE_TYPE_RSA,hex(self._signature[0]))
        else:
            raise Exception("unknown signature type %s"%self._type)
        
    def loads(self,string):
        """load signature param from string
        do not raise exception if errors"""
        try:
            type,data = string.split(':')
        except:
            warningOutput("bad signature format : %s"%string)
            return
        if type == SIGNATURE_TYPE_RSA:
            self._type = SIGNATURE_TYPE_RSA
            self._signature = (eval(data),)
        else:
            warningOutput("bad signature format : %s"%string)
            return
        
    def getValue(self):
        return self._signature
    
    def getType(self):
        return self._type